﻿using System;
using System.Linq;
using System.Web.Mvc;

namespace SusuCMS.Web
{
    public class AdminControllerBase : Controller
    {
        public IUnitOfWork UnitOfWork
        {
            get
            {
                if (_UnitOfWork == null)
                {
                    _UnitOfWork = CmsApplication.GetDataContext();
                }

                return _UnitOfWork;
            }
        }
        private IUnitOfWork _UnitOfWork;

        public IUserRepository UserRepository
        {
            get
            {
                if (_UserRepository == null)
                {
                    _UserRepository = CmsApplication.GetUserRepository(UnitOfWork);
                }

                return _UserRepository;
            }
        }
        private IUserRepository _UserRepository;

        public IPageRepository PageRepository
        {
            get
            {
                if (_PageRepository == null)
                {
                    _PageRepository = CmsApplication.GetPageRepository(UnitOfWork);
                }

                return _PageRepository;
            }
        }
        private IPageRepository _PageRepository;

        public IPageTypeRepository PageTypeRepository
        {
            get
            {
                if (_PageTypeRepository == null)
                {
                    _PageTypeRepository = CmsApplication.GetPageTypeRepository(UnitOfWork);
                }

                return _PageTypeRepository;
            }
        }
        private IPageTypeRepository _PageTypeRepository;

        public IWebsiteRepository WebsiteRepository
        {
            get
            {
                if (_WebsiteRepository == null)
                {
                    _WebsiteRepository = CmsApplication.GetWebsiteRepository(UnitOfWork);
                }

                return _WebsiteRepository;
            }
        }
        private IWebsiteRepository _WebsiteRepository;

        public User CurrentUser
        {
            get
            {
                if (_CurrentUser == null)
                {
                    _CurrentUser = UserRepository.FindAll()
                        .FirstOrDefault(i => i.UserName == HttpContext.User.Identity.Name);
                }

                return _CurrentUser;
            }
        }
        private User _CurrentUser;

        public int CurrentWebsiteId
        {
            get
            {
                if (ControllerContext.RouteData.Values.ContainsKey("websiteId"))
                {
                    return Convert.ToInt32(ControllerContext.RouteData.Values["websiteId"]);
                }

                return 0;
            }
        }

        public Website CurrentWebsite
        {
            get
            {
                if (_CurrentWebsite == null)
                {
                    _CurrentWebsite = WebsiteRepository.FindAll()
                        .Where(i => i.IsDeleted == false)
                        .FirstOrDefault(i => i.Id == CurrentWebsiteId);
                }
                return _CurrentWebsite;
            }
        }
        private Website _CurrentWebsite;

        public int CurrentPageTypeId
        {
            get
            {
                if (ControllerContext.RouteData.Values.ContainsKey("pageTypeId"))
                {
                    return Convert.ToInt32(ControllerContext.RouteData.Values["pageTypeId"]);
                }

                return 0;
            }
        }

        protected ActionResult RedirectToIndex()
        {
            var returnUrl = Request.QueryString["returnUrl"];
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }

            return RedirectToAction("Index");
        }

        protected new ActionResult HttpNotFound()
        {
            return RedirectToAction("NotFound", "Shared");
        }
    }
}
